//
//  WSDataReportHelper.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/11.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSDataReportHelper.h"

@implementation WSDataReportHelper

//将字典转换成json格式字符串,不含\n这些符号
+ (NSString *)jsonStringForDictionary:(NSDictionary *)dictionary {
    if (![dictionary isKindOfClass:[NSDictionary class]]||
        ![NSJSONSerialization isValidJSONObject:dictionary]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}

 
//将数组转换成json格式字符串,不含\n这些符号
+ (NSString *)jsonStringForArray:(NSArray *)array {
    if (![array isKindOfClass:[NSArray class]] ||
        ![NSJSONSerialization isValidJSONObject:array]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}

 
//将字典转换成json格式字符串,含\n这些符号,便于阅读

+ (NSString *)jsonStringPrettyPrintedFormatForDictionary:(NSDictionary *)dictionary {
    if (![dictionary isKindOfClass:[NSDictionary class]] || ![NSJSONSerialization isValidJSONObject:dictionary]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return strJson;
}


// ios 判断字符串为空和只为空格解决办法
+ (BOOL)isBlankString:(NSString *)string {
    if (string == nil) {
        return YES;
    }
    if (string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    if ([[string stringByTrimmingCharactersInSet:characterSet] length] == 0) {
        return YES;
    }
    return NO;
    
}


+ (NSMutableArray*)calculateSubStringCount:(NSString *)content str:(NSString *)tab {
    int location = 0;
    NSMutableArray *locationArr = [NSMutableArray new];
    NSRange range = [content rangeOfString:tab];
    if (range.location == NSNotFound){
        return locationArr;
    }
    //声明一个临时字符串,记录截取之后的字符串
    NSString * subStr = content;
    while (range.location != NSNotFound) {
        if (location == 0) {
            location += range.location;
        } else {
            location += range.location + tab.length;
        }
        //记录位置
        NSNumber *number = [NSNumber numberWithUnsignedInteger:location];
        [locationArr addObject:number];
        //每次记录之后,把找到的字串截取掉
        subStr = [subStr substringFromIndex:range.location + range.length];
        range = [subStr rangeOfString:tab];
    }
    return locationArr;
}


+ (BOOL)isContainsResultObject:(NSArray<NSString *> *)array withResultObject:(NSString *)resultObject {
    for (NSString *str in array) {
        if ([resultObject containsString:str]) {
            return YES;
        }
    }
    return NO;
}

+ (BOOL)isEmptyForObject:(NSObject *)object {
    return !object || [object isKindOfClass:[NSNull class]];
}

+ (BOOL)nonContainerClassObject:(NSObject *)object {
    return [object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]];
}

+ (BOOL)isContainerClassObject:(NSObject *)object {
    return [object isKindOfClass:[NSDictionary class]] || [object isKindOfClass:[NSArray class]];
}

+ (NSString *)jsonStringForContainerClassObject:(NSObject *)object {
    NSString *resultObject = @"";
    if ([object isKindOfClass:[NSDictionary class]]) {
        resultObject = [WSDataReportHelper jsonStringForDictionary:(NSDictionary *)object];
    } else if ([object isKindOfClass:[NSArray class]]) {
        resultObject = [WSDataReportHelper jsonStringForArray:(NSArray *)object];
    }
    return resultObject;
}

+ (NSString *)stringValueForNonContainerClassObject:(NSObject *)object {
    NSString *resultObject = @"";
    if ([object isKindOfClass:[NSString class]]) {
        return (NSString *)object;
    } else if ([object isKindOfClass:[NSNumber class]]) {
        return ((NSNumber *)object).stringValue;;
    }
    return resultObject;
}


///与视图同等相关的约束
+ (NSLayoutConstraint *)equallyRelatedConstraintWithView:(UIView *)sourceView
                                                  toItem:(UIView *)targetView
                                               attribute:(NSLayoutAttribute)attribute
{
    return [NSLayoutConstraint constraintWithItem:sourceView
                                        attribute:attribute
                                        relatedBy:NSLayoutRelationEqual
                                           toItem:targetView
                                        attribute:attribute
                                       multiplier:1.0
                                         constant:0.0];
}


@end
